package com.zwr.sheller.customer.controller;

import com.zwr.sheller.core.common.JsonResult;
import com.zwr.sheller.core.common.WDMBusinessException;
import com.zwr.sheller.core.pojo.Address;
import com.zwr.sheller.customer.Service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author wdm
 */
@RestController
@RequestMapping("/selectAdderss")
@Api("订单模块中处理收获地址")
public class AddressController {
    @Autowired(required = false)
    AddressService addressService;

    @PostMapping("/selectAddressByUser")
    @ApiOperation(value = "订单模块中选择地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Long"),
    })
    public Map<String ,Object> selectAddressByUser(Long userId) throws WDMBusinessException {
        List<Address> addresses = addressService.selectAddressByUserId(userId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setField("Data",addresses);
        return jsonResult.getMap();
    }


    @PostMapping("/addAddress")
    @ApiOperation(value = "新增地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressName",value = "收货人姓名",dataType = "String"),
            @ApiImplicitParam(name = "addressPhone",value = "联系电话",dataType = "String"),
            @ApiImplicitParam(name = "addressArea",value = "地区",dataType = "String"),
            @ApiImplicitParam(name = "addressDetail",value = "地址详情",dataType = "String"),
            @ApiImplicitParam(name = "addressPostCode",value = "地址邮编",dataType = "String"),
            @ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Long"),
    })
    public Map<String,Object> addAddress(Address address) throws WDMBusinessException {
        addressService.add(address);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }

    @PostMapping("/updateAddress")
    @ApiOperation(value = "修改地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressId",value = "需要被修改的地址Id",dataType = "Long"),
            @ApiImplicitParam(name = "addressName",value = "收货人姓名",dataType = "String"),
            @ApiImplicitParam(name = "addressPhone",value = "联系电话",dataType = "String"),
            @ApiImplicitParam(name = "addressArea",value = "地区",dataType = "String"),
            @ApiImplicitParam(name = "addressDetail",value = "地址详情",dataType = "String"),
            @ApiImplicitParam(name = "addressPostCode",value = "地址邮编",dataType = "String"),
            @ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Long"),
    })
    public Map<String,Object> updateAddress(Address address) throws WDMBusinessException {
        addressService.update(address);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }


    @PostMapping("/selectOneAddress")
    @ApiOperation(value = "查找单个地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressId",value = "需要被修改的地址Id",dataType = "Long"),
    })
    public  Map<String,Object> selectOneAddress(Long addressId) throws WDMBusinessException {
        Address newAddress = new Address();
        newAddress.setAddressId(addressId);
        Address address = addressService.selectByFeatures(newAddress);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setField("Data",address);
        return jsonResult.getMap();
    }

}
